package gcu.module.room.controller;

import gcu.module.build.daomain.Build;
import gcu.module.build.service.BuildService;
import gcu.module.room.daomain.Room;
import gcu.module.room.service.RoomService;
import gcu.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by haol on 2016/9/26.
 */
@Controller
@RequestMapping("/room")
public class RoomController {
      @Autowired
      private RoomService roomService;
      @Autowired
      private BuildService buildService;

      @RequestMapping("/list")
      public String list(Model model, Room room, Pager pager){
            List<Room> rooms = roomService.queryRoomList(room,pager);
            model.addAttribute("roomList",rooms);

            List<Build> build_list = buildService.queryBuild();
            model.addAttribute("build_list",build_list);

            model.addAttribute("pager",pager);
            return "/room/list";
      }
      @RequestMapping("/list/{id}")
      public String listByBuild(Model model, Room room, Pager pager,@PathVariable int id){
            Build build = buildService.queryBuildById(id);
            room.setBuildName(build.getBuildName());
            List<Room> rooms = roomService.queryRoomList(room,pager);

            List<Build> build_list = buildService.queryBuild();
            model.addAttribute("build_list",build_list);

            model.addAttribute("roomList",rooms);
            model.addAttribute("pager",pager);
            model.addAttribute("buildId",id);
            return "/room/list";
      }

      @RequestMapping("/edit/{id}")
      public String edit(@PathVariable Integer id, Model model) {
            model.addAttribute("id",id);
            return "/room/edit";
      }

      @RequestMapping("/add")
      public String add(Model model) {

            List<Build> build_list = buildService.queryBuild();
            model.addAttribute("build_list",build_list);

            model.addAttribute("room", new Room());
            return "/room/edit";
      }
      /*如果是从楼宇查看详情，则存入楼宇id*/
      @RequestMapping("/add/{id}")
      public String addByBuild(Model model,@PathVariable int id) {
            model.addAttribute("room", new Room());
            model.addAttribute("id",null);
            model.addAttribute("buildId",id);
            return "/room/edit";
      }

      @RequestMapping("/del/{id}")
      public String del(@PathVariable int id) {
          roomService.deleteRoom(id);
            return "redirect:/room/list";
      }

}
